package com.lzy.cloud.ticket.train.facade;

import com.lzy.cloud.ticket.train.dtos.TrainExpressInfoDto;
import com.lzy.cloud.ticket.train.dtos.TrainOrderDto;
import com.lzy.cloud.ticket.train.dtos.TrainPassengerDto;
import com.lzy.cloud.ticket.train.remote.OrderApi;
import com.lzy.cloud.ticket.train.remote.request.OrderInfoRequest;
import com.lzy.cloud.ticket.train.request.app.OrderBookingCallbackAppRequest;
import com.lzy.cloud.ticket.train.response.BookingOrderCallBackResponse;
import com.lzy.cloud.ticket.train.response.CancleOrderCallBackResponse;
import com.lzy.cloud.ticket.train.servcie.TrainChangsService;
import com.lzy.cloud.ticket.train.servcie.TrainExpressInfoService;
import com.lzy.cloud.ticket.train.servcie.TrainOrderService;
import com.lzy.cloud.ticket.train.servcie.TrainPassengerService;
import com.lzy.cloud.ticket.train.util.BasePushUtil;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.lzy.cloud.ticket.train.constant.TrainOrderStatus.*;
import static com.lzy.cloud.ticket.train.remote.constant.OrderStatus.*;

/**
 * 回调facade
 *
 * @author lc
 * @version 2018/8/3
 */
@Component
@Slf4j
public class TrainCallBackFacade {

    @Autowired
    private TrainOrderService trainOrderService;

    @Autowired
    private TrainExpressInfoService trainExpressInfoService;

    @Autowired
    private TrainPassengerService trainPassengerService;

    @Autowired
    private TrainChangsService trainChangsService;

    @Autowired
    private OrderApi orderApi;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private BasePushUtil basePushUtil;

    /**
     * 预定回调
     * @param response
     * @return
     */
    public Result bookingCallback(BookingOrderCallBackResponse response) {
        if (!response.getCode().equals("1")){
            return new Result(response.getCode(),response.getErrorMessage(),response.getData());
        }

        //更改订单总表
        OrderInfoRequest orderInfoRequest = new OrderInfoRequest();
        orderInfoRequest.setOrderId(response.getData().getPartnerOrderNo());
        orderInfoRequest.setMaxStatus(MAXSTATUS_VALID);
        orderInfoRequest.setMinStatus(MINSTATUS_COMMITTED);
        Result orderResult = orderApi.update(null,orderInfoRequest);
        if ("0" != orderResult.getCode()){
            return orderResult;
        }

        Result result = new Result(updateTrainOrder(response));
        return result;
    }

    public Boolean updateTrainOrder(BookingOrderCallBackResponse bookingOrderCallBackResponse) {

        final OrderBookingCallbackAppRequest orderBookingCallbackRequest = bookingOrderCallBackResponse.getData();
        Ensure.that(orderBookingCallbackRequest).isNotNull("00010006");


        TrainOrderDto trainOrderDto = DozerUtils.map(orderBookingCallbackRequest,TrainOrderDto.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trainOrderDto.setUpdateTime(sdf.format(new Date()));

        List<TrainPassengerDto> list = DozerUtils.maps(orderBookingCallbackRequest.getOrderBookingPassengerVos(),TrainPassengerDto.class);
        trainOrderDto.setOrderBookingPassengerInfos(list);

        //驴妈妈返回参数映射库表
        //自己的订单ID
        trainOrderDto.setCustomId(trainOrderDto.getPartnerOrderNo());
        //驴妈妈订单ID
        trainOrderDto.setOrderId(trainOrderDto.getOrderNo());
        //12306的订单号
        trainOrderDto.setOrderNumber(trainOrderDto.getElectronicTicketNumber());

        //根据CustomId得到TrainOrderDto,
        TrainOrderDto trainOrder = trainOrderService.queryTrainOrderByCondition(trainOrderDto.getCustomId());
        long trainOrderId = 0L;
        if (trainOrder.getId() != null){
            trainOrderId = trainOrder.getId();
        }
        trainOrderDto.setId(trainOrder.getId());
        trainOrderService.updateTrainOrder(trainOrderDto);
        //保险信息编辑
        /*for (TrainPassengerDto trainPassengerDto:trainOrderDto.getOrderBookingPassengerVos()) {
            if (trainPassengerDto.getInsuranceVos() != null && trainPassengerDto.getInsuranceVos().size() > 0) {
                for (TrainInsuranceDto trainInsuranceDto : trainPassengerDto.getInsuranceVos()) {
                    trainInsuranceDto.setInsuranceNo(trainOrderId);
                    trainInsuranceService.updateTrainInsurance(trainInsuranceDto);
                }

            }
        }*/
        //物流信息编辑
        if (trainOrderDto.getExpressInfo() != null) {
            TrainExpressInfoDto trainExpressInfo = trainOrderDto.getExpressInfo();
            trainExpressInfo.setTrainOrderId(trainOrderId);
            trainExpressInfoService.updateTrainExpressInfo(trainExpressInfo);
        }
        //乘客信息编辑
        if (trainOrderDto.getOrderBookingPassengerInfos() != null && trainOrderDto.getOrderBookingPassengerInfos().size() > 0) {
            for (TrainPassengerDto trainPassengerDto : trainOrderDto.getOrderBookingPassengerInfos()) {
                trainPassengerDto.setTrainOrderId(trainOrderId);
                trainPassengerService.updateTrainPassenger(trainPassengerDto);
            }

        }
        return true;
    }

    /**
     * 订单取消回调接口
     *
     * @param response
     */
    public Result cancelTicketCallback(CancleOrderCallBackResponse response) {
        if ("1" != response.getCode()){
            return new Result(response.getCode(),response.getErrorMessage(),response.getData());
        }

        //更改订单总表
        OrderInfoRequest orderInfoRequest = new OrderInfoRequest();
        orderInfoRequest.setOrderId(response.getData().getPartnerOrderNo());
        orderInfoRequest.setMaxStatus(MAXSTATUS_FINISHED);
        orderInfoRequest.setMinStatus(MINSTATUS_CANCELED);
        Result orderResult = orderApi.update(null,orderInfoRequest);
        if ("0" != orderResult.getCode()){
            return orderResult;
        }

        TrainOrderDto trainOrderDto = new TrainOrderDto();
        trainOrderDto.setCustomId(response.getData().getPartnerOrderNo());
        trainOrderDto.setOrderId(response.getData().getOrderNo());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trainOrderDto.setCancelTime(sdf.format(new Date()));
        trainOrderDto.setStatus(CANCELED_TICKET);
        trainOrderDto.setId(trainOrderService.queryTrainOrderByCondition(response.getData().getPartnerOrderNo()).getId());

        return new Result(trainOrderService.updateTrainOrder(trainOrderDto));
    }

    /**
     * 确认出票
     * @param response
     * @return
     */
    public Result confirmTicketCallback(BookingOrderCallBackResponse response) {
        if (!response.getCode().equals("1")){
            return new Result(response.getCode(),response.getErrorMessage(),response.getData());
        }

        //更改订单总表
        OrderInfoRequest orderInfoRequest = new OrderInfoRequest();
        orderInfoRequest.setOrderId(response.getData().getPartnerOrderNo());
        orderInfoRequest.setMaxStatus(MAXSTATUS_VALID);
        orderInfoRequest.setMinStatus(MINSTATUS_SUCCESS);
        Result orderResult = orderApi.update(null,orderInfoRequest);
        if ("0" != orderResult.getCode()){
            return orderResult;
        }

        Result result = new Result(updateTrainOrder(response));
        return result;
    }

    /**
     * 退票回调
     * @param response
     * @return
     */
    public Result returnTicketCallback(BookingOrderCallBackResponse response) {
        if (!response.getCode().equals("1")){
            return new Result(response.getCode(),response.getErrorMessage(),response.getData());
        }

        //更改订单总表
        OrderInfoRequest orderInfoRequest = new OrderInfoRequest();
        orderInfoRequest.setOrderId(response.getData().getPartnerOrderNo());
        orderInfoRequest.setMaxStatus(MAXSTATUS_FINISHED);
        orderInfoRequest.setMinStatus(MINSTATUS_RETURNED);
        Result orderResult = orderApi.update(null,orderInfoRequest);
        if ("0" != orderResult.getCode()){
            return orderResult;
        }

        Result result = new Result(updateTrainOrder(response));
        return result;
    }

    /**
     * 改签回调
     * @param response
     * @return
     */
    public Result changeTicketCallback(BookingOrderCallBackResponse response) {
        if (!response.getCode().equals("1")){
            return new Result(response.getCode(),response.getErrorMessage(),response.getData());
        }

        //更改订单总表
        OrderInfoRequest orderInfoRequest = new OrderInfoRequest();
        orderInfoRequest.setOrderId(response.getData().getPartnerOrderNo());
        orderInfoRequest.setMaxStatus(MAXSTATUS_VALID);
        orderInfoRequest.setMinStatus(MINSTATUS_CHANGED);
        Result orderResult = orderApi.update(null,orderInfoRequest);
        if ("0" != orderResult.getCode()){
            return orderResult;
        }

        Result result = new Result(updateTrainOrder(response));
        return result;
    }
}
